
package com.ykdl.member.kid.adapters;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.adapters.MyChatMsgViewAdapter.ViewHolder;
import com.ykdl.member.kid.beans.DoctorAnswerBean;
import com.ykdl.member.kid.circle.LookImageActivity;
import com.ykdl.member.kid.doctor.DoctorInfoActivity;
import com.ykdl.member.kid.doctor.DoctorOnlineActivity;
import com.ykdl.member.kid.image.ImageOptions;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.models.FileMetas;
import com.ykdl.member.kid.util.ChatMsgEntity;
import com.ykdl.member.kid.widget.AsyncImageView;
/**
 * 聊天Adapter
 * @author chenshuai
 */
public class ChatMsgViewAdapter extends BaseAdapter {
	private ImageOptions options=null ;
	private String TAG = "ChatMsgViewAdapter";
	public static interface IMsgViewType
	{
		int IMVT_COM_MSG = 0;
		int IMVT_TO_MSG = 1;
	}
	private ArrayList<DoctorAnswerBean>  list;

    private List<ChatMsgEntity> coll;

    private Context ctx;
    
    private LayoutInflater mInflater;
    private BitmapUtils bitmapUtil ;
    public ChatMsgViewAdapter(Context context) {
        ctx = context;
        mInflater = LayoutInflater.from(ctx);
        options = ImageOptionsFactory.getBigPortraitOptions(ctx);
        bitmapUtil = new BitmapUtils(context);
        bitmapUtil.configDefaultLoadingImage(R.drawable.defualt_doctor_icon);
		bitmapUtil.configDefaultLoadFailedImage(R.drawable.defualt_doctor_icon);
    }
    public void setList(ArrayList<DoctorAnswerBean>  list) {
		this.list = list;
		initData();
	}
	public void initData() {
		List<ChatMsgEntity> mDataArrays = new ArrayList<ChatMsgEntity>();
		for (int i = 0; i < list.size(); i++) {
			ChatMsgEntity entity = new ChatMsgEntity();
			entity.setDate(list.get(i).getPost_time());
			entity.setBean(list.get(i).getPost_actor());
			if (list.get(i).getPost_actor().getActor_type()==3) {
				entity.setMsgType(true);
			} else {
				entity.setMsgType(false);
			}
			if (list.get(i).isHas_image()) {
				entity.setHasImage(true);
				entity.setFile_metas(list.get(i).getFile_metas());
			}else {
				entity.setHasImage(false);
			}

			entity.setText(list.get(i).getMessage());
			mDataArrays.add(entity);
		}
		coll = mDataArrays;

	}
    public int getCount() {
        return coll.size();
    }

    public Object getItem(int position) {
        return coll.get(position);
    }

    public long getItemId(int position) {
        return position;
    }
    


	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
	 	ChatMsgEntity entity = coll.get(position);
	 	
	 	if (entity.getMsgType())
	 	{
	 		return IMsgViewType.IMVT_COM_MSG;
	 	}else{
	 		return IMsgViewType.IMVT_TO_MSG;
	 	}
	 	
	}


	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 2;
	}
	
	
    public View getView(int position, View convertView, ViewGroup parent) {
    	
    	ChatMsgEntity entity = coll.get(position);
    	boolean isComMsg = entity.getMsgType();
    		
    	ViewHolder viewHolder = null;	
	    if (convertView == null)
	    {
	    	  if (isComMsg)
			  {
				  convertView = mInflater.inflate(R.layout.chatting_item_msg_text_left, null);
			  }else{
				  convertView = mInflater.inflate(R.layout.chatting_item_msg_text_right, null);
			  }

	    	  viewHolder = new ViewHolder();
			  viewHolder.tvUserName = (TextView) convertView.findViewById(R.id.tv_username);
			  viewHolder.tvContent = (TextView) convertView.findViewById(R.id.tv_chatcontent);
			  viewHolder.topic_picture1 =  (ImageView) convertView.findViewById(R.id.topic_picture1);
			  viewHolder.topic_picture2 =  (ImageView) convertView.findViewById(R.id.topic_picture2);
			  viewHolder.topic_picture3 =  (ImageView) convertView.findViewById(R.id.topic_picture3);
			  viewHolder.topic_picture4 =  (ImageView) convertView.findViewById(R.id.topic_picture4);
			  viewHolder.isComMsg = isComMsg;
			  viewHolder.iv_userhead =  (ImageView) convertView.findViewById(R.id.iv_userhead);
			  viewHolder.user_grade = (TextView) convertView.findViewById(R.id.user_grade);
			  convertView.setTag(viewHolder);
	    }else{
	        viewHolder = (ViewHolder) convertView.getTag();
	    }
	
	    
	    
	    if (entity.getBean().getAvatar_file_meta()!=null) {
	    	String url = entity.getBean().getAvatar_file_meta().getDownload_urls().getSmal().getUrl();
	    	bitmapUtil.display(viewHolder.iv_userhead, url);
		}else {
			viewHolder.iv_userhead.setImageResource(R.drawable.defualt_doctor_icon);
		}
	    viewHolder.tvUserName.setText(entity.getBean().getDisplay_name());
	    viewHolder.tvContent.setText(entity.getText());
	    if (isComMsg) {
	    	viewHolder.user_grade.setVisibility(View.INVISIBLE);
	    	viewHolder.iv_userhead.setTag(entity.getBean().getActor_id());
	    	viewHolder.iv_userhead.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(ctx, DoctorOnlineActivity.class);
					intent.putExtra("doctor_id", v.getTag()+"");
					ctx.startActivity(intent);
				}
			});
		}else {
			viewHolder.user_grade.setVisibility(View.VISIBLE);
			viewHolder.user_grade .setText(entity.getBean().getLevel()+"");
		}
	    
	    if (entity.isHasImage()) {
	    	doholderpicdata(viewHolder,
	    			entity.getFile_metas()) ;
		}else {
		}
	    return convertView;
    }
    public void doholderpicdata(ViewHolder holder,
			 final ArrayList<FileMetas> file_meta) {
		if (file_meta.size() == 1) {
			holder.topic_picture1.setVisibility(View.VISIBLE);
			String url = file_meta.get(0).getDownload_urls().getSmal().getUrl();
			Log.e("alan", "-------------------------------url:+url");
			bitmapUtil.display(holder.topic_picture1, url);
		}
		if (file_meta.size() == 2) {
			holder.topic_picture1.setVisibility(View.VISIBLE);
			holder.topic_picture2.setVisibility(View.VISIBLE);
			String url = file_meta.get(0).getDownload_urls().getSmal().getUrl();
			bitmapUtil.display(holder.topic_picture1, url);
			String url2 = file_meta.get(1).getDownload_urls().getSmal().getUrl();
			bitmapUtil.display(holder.topic_picture2, url2);
		}
		if (file_meta.size() == 3) {
			holder.topic_picture1.setVisibility(View.VISIBLE);
			String url = file_meta.get(0).getDownload_urls().getSmal().getUrl();
			bitmapUtil.display(holder.topic_picture1, url);
			holder.topic_picture2.setVisibility(View.VISIBLE);
			String url2 = file_meta.get(1).getDownload_urls().getSmal().getUrl();
			bitmapUtil.display(holder.topic_picture2, url2);
			holder.topic_picture3.setVisibility(View.VISIBLE);
			String url3 = file_meta.get(2).getDownload_urls().getSmal().getUrl();
			bitmapUtil.display(holder.topic_picture3, url3);
		}
		if (file_meta.size() == 4) {
			holder.topic_picture1.setVisibility(View.VISIBLE);
			String url = file_meta.get(0).getDownload_urls().getSmal().getUrl();
			bitmapUtil.display(holder.topic_picture1, url);
			holder.topic_picture2.setVisibility(View.VISIBLE);
			String url2 = file_meta.get(1).getDownload_urls().getSmal().getUrl();
			bitmapUtil.display(holder.topic_picture2, url2);
			holder.topic_picture3.setVisibility(View.VISIBLE);
			String url3 = file_meta.get(2).getDownload_urls().getSmal().getUrl();
			bitmapUtil.display(holder.topic_picture3, url3);
			holder.topic_picture4.setVisibility(View.VISIBLE);
			String url4 = file_meta.get(3).getDownload_urls().getSmal().getUrl();
			bitmapUtil.display(holder.topic_picture4, url4);
		}
		holder.topic_picture1
				.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						lookbigimage(file_meta, 0);
					}
				});
		holder.topic_picture2
				.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						lookbigimage(file_meta, 1);
					}
				});
		holder.topic_picture3
				.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						lookbigimage(file_meta, 2);
					}
				});
		
	}

    static class ViewHolder { 
    	 public TextView tvUserName,tvSendTime,tvContent,user_grade;
         public ImageView topic_picture1,topic_picture2,topic_picture3,topic_picture4,
         iv_userhead;
         LinearLayout topic_user_pic ;
         public boolean isComMsg = true;
    }
    public void lookbigimage(ArrayList<FileMetas> file_meta,int position) {
		Intent intent = new Intent(ctx, LookImageActivity.class);
		intent.putExtra("list",  (Serializable)file_meta);
		intent.putExtra("position",  position);
		ctx.startActivity(intent);
	}

}
